import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/home/home_page.dart';
import 'package:flutter_enjoy_plus/pages/mine/mine_page.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class TabbarPage extends StatefulWidget {
  const TabbarPage({Key? key}) : super(key: key);

  @override
  _TabbarPageState createState() => _TabbarPageState();
}

class _TabbarPageState extends State<TabbarPage> {
  List tabsList = [
    {
      'label': '首页',
      'icon': 'assets/tabs/home_default.png',
      'activeIcon': 'assets/tabs/home_active.png'
    },
    {
      'label': '我的',
      'icon': 'assets/tabs/my_default.png',
      'activeIcon': 'assets/tabs/my_active.png'
    }
  ];
  @override
  void initState() {
    super.initState();
    //初始化token
    tokenManager.initToken();
  }

  int activeIndex = 1;

  // List<BottomNavigationBarItem> getTabsBar() {
  //   List<BottomNavigationBarItem> list = [];
  //   for (var i = 0; i < tabsList.length; i++) {
  //     list.add(
  //       BottomNavigationBarItem(
  //           label: tabsList[i]["label"],
  //           icon: Image.asset(
  //             tabsList[i]["icon"],
  //             width: 30,
  //             height: 30,
  //           ),
  //           activeIcon: Image.asset(
  //             tabsList[i]["activeIcon"],
  //             width: 30,
  //             height: 30,
  //           )),
  //     );
  //   }
  //   return list;
  // }
  List<BottomNavigationBarItem> getTabsBar() {
    List<BottomNavigationBarItem> list = [];
    for (var i = 0; i < tabsList.length; i++) {
      list.add(
        BottomNavigationBarItem(
            label: tabsList[i]['label'],
            icon: Image.asset(tabsList[i]['icon'], width: 30, height: 30),
            activeIcon: Image.asset(
              tabsList[i]['activeIcon'],
              width: 30,
              height: 30,
            )),
      );
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: activeIndex,
        children: [HomePage(), MinePage()],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: activeIndex,
        selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
        unselectedItemColor: Colors.black,
        items: getTabsBar(),
        onTap: (int index) {
          setState(() {
            activeIndex = index;
          });
        },
      ),
    );
  }

  // @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //     body: Text("TabbarPage"),
  //     bottomNavigationBar: BottomNavigationBar(
  //       type: BottomNavigationBarType.fixed,
  //       currentIndex: activeIndex,
  //       selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
  //       unselectedItemColor: Colors.black,
  //       items: getTabsBar(),
  //       onTap: (int index) {
  //         setState(() {
  //           activeIndex = index;
  //         });
  //       },
  //     ),
  //   );
  // }
}
